package scsc.web.oa.consult.actions;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import scsc.pm.ConsultPm;
import scsc.pm.SysDictItemUtil;
import scsc.pub.data.UserContext;
import scsc.pub.db.DBManage;
import scsc.pub.db.PageListData;
import scsc.pub.exception.UnifyUserException;
import scsc.pub.tools.StringUtil;
import scsc.web.oa.consult.data.ConsultData;
import scsc.web.oa.consult.forms.ConsultForm;
import scsc.web.system.AppAction;
import scsc.web.system.Constants;

public class ConsultAction extends AppAction {
	public ConsultAction() {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ConsultForm actionForm = (ConsultForm) form;//
		ConsultPm pm = new ConsultPm();
		String fwd = null;
		ActionErrors errors = new ActionErrors();
		HttpSession session = request.getSession();
		UserContext userContext = (UserContext) session
				.getAttribute("userContext");
		DBManage dbmanage = null;

		String operAtt = actionForm.getOperAtt();
		if (operAtt == null || StringUtil.trim(operAtt).equals("")) {
			operAtt = Constants.DB_Select;
		}

		if (operAtt.equals(Constants.DB_Select)) {
			try {
				Integer bulletinId = actionForm.getData().getBulletinId();
				PageListData footer = actionForm.getFooter();
				dbmanage = new DBManage();
				pm.selectConsults(dbmanage, bulletinId, footer);
				dbmanage.close();
				fwd = Constants.Consult_Perform_Select_Success_Forward;
			} catch (UnifyUserException e) {
				dbmanage.close();
				errors.add("ConsultSession", new ActionError(
						Constants.Error_DB_Select, e.getErrorMsg()));

				fwd = Constants.Consult_Perform_Select_Failure_Forward;
			}
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
		}
		return mapping.findForward(fwd);
	}
}
